#ifndef U2MemoryAllocatedObject_H
#define U2MemoryAllocatedObject_H

#include "U2PreRequest.h"


#ifdef new
#  undef new
#endif
#ifdef delete
#  undef delete
#endif


U2EG_NAMESPACE_BEGIN


template <class Alloc>
class _U2Share U2MemoryAllocatedObject
{
public:
	explicit U2MemoryAllocatedObject()
	{ }

	~U2MemoryAllocatedObject()
	{ }

	/// operator new, with debug line info
	void* operator new(size_t sz, const char* file, int line, const char* func)
	{
		return Alloc::allocateBytes(sz, file, line, func);
	}

	void* operator new(size_t sz)
	{
		return Alloc::allocateBytes(sz);
	}

	/// placement operator new
	void* operator new(size_t sz, void* ptr)
	{
		return ptr;
	}

	/// array operator new, with debug line info
	void* operator new[] ( size_t sz, const char* file, int line, const char* func )
	{
		return Alloc::allocateBytes(sz, file, line, func);
	}

	void* operator new[] ( size_t sz )
	{
		return Alloc::allocateBytes(sz);
	}

	void operator delete( void* ptr )
	{
		Alloc::deallocateBytes(ptr);
	}

	// Corresponding operator for placement delete (second param same as the first)
	void operator delete( void* ptr, void* )
	{
		Alloc::deallocateBytes(ptr);
	}

	// only called if there is an exception in corresponding 'new'
	void operator delete( void* ptr, const char* , int , const char*  )
	{
		Alloc::deallocateBytes(ptr);
	}

	void operator delete[] ( void* ptr )
	{
		Alloc::deallocateBytes(ptr);
	}

	void operator delete[] ( void* ptr, const char* , int , const char*  )
	{
		Alloc::deallocateBytes(ptr);
	}
};


U2EG_NAMESPACE_END

#endif